import jwt from "jsonwebtoken";
const secret = "fgjgjgjgkgkgkkgj";
export let createToken = (data: any) => {
  //创建token的方法
  let obj: any = {};
  obj.data = data || {}; //存入token的数据
  obj.ctime = new Date().getTime(); //token的创建时间
  obj.expiresIn = 1000 * 60 * 60 * 24 * 7; //设定的过期时间
  let token = jwt.sign(obj, secret);
  return token;
};
export let varifyToken = (token: string) => {
  //验证token是否合法的方法
  let result = null;
  try {
    let { data, ctime, expiresIn } = jwt.verify(token, secret) as any;
    let nowTime = new Date().getTime();
    if (nowTime - ctime < expiresIn) {
      result = data;
    }
  } catch (error) {}
  //console.log(result)
  return result;
};
